
// src/api/console.ts
import { apiClient } from './client';
import { ConsoleInfo } from './types';

export const consoleApi = {
  // 获取控制台类型
  getConsoleTypes: async (vmName: string, connectionId: string) => {
    const { data } = await apiClient.get<string[]>(
      `/console/${connectionId}/${vmName}/types`
    );
    return data;
  },

  // 获取串行控制台信息
  getSerialInfo: async (vmName: string, connectionId: string) => {
    const { data } = await apiClient.get<ConsoleInfo>(
      `/console/serial/${connectionId}/${vmName}/info`
    );
    return data;
  },

  // 获取 VNC 信息
  getVNCInfo: async (vmName: string, connectionId: string) => {
    const { data } = await apiClient.get<ConsoleInfo>(
      `/console/vnc/${connectionId}/${vmName}/info`
    );
    return data;
  },

  // 获取 SPICE 信息
  getSPICEInfo: async (vmName: string, connectionId: string) => {
    const { data } = await apiClient.get<ConsoleInfo>(
      `/console/spice/${connectionId}/${vmName}/info`
    );
    return data;
  },

  // 构建 WebSocket URL
  buildWebSocketURL: (path: string) => {
    const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
    const host = API_BASE_URL.replace(/^https?:\/\//, '');
    return `${protocol}//${host}/api/v1${path}`;
  },
};